package com.tensquare.service;

/**
 * @Description:
 * @Author: xuxiaoyu
 * @Date: 2019/11/15 11:25
 */
import com.tensquare.dao.LabelDao;
import com.tensquare.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *不使用接口开发,因为我们使用的是微服务架构
 *把每个具体的业务逻辑抽取为一个独立的微服务
 *一个接口有多个实现,现在的做法应该是使用多个微服务
 *所以现在的接口开发就没有意义,可以不使用了
 *本项目就不适用接口开发
 */
@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 条件查询
     */
    public List<Label> findSearch(Map searchMap){
        Specification specification = createSpecification(searchMap);
        return labelDao.findAll(specification);
    }
    public Page<Label> findSearch(Map searchMap,int page,int limit){
        Specification<Label> specification = createSpecification(searchMap);
        //Pageable pageable = new PageRequest(page,limit);
        PageRequest pageRequest = PageRequest.of(page-1, limit);
        return labelDao.findAll(specification, pageRequest);
    }

    /**
     * 查询全部标签
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 根据id查询标签
     * @param id
     * @return
     */
    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    /**
     * 添加新的标签
     * @param label
     */
    public void add(Label label){
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }

    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }
    @Transactional
    public void deleteById(String id){
        labelDao.deleteById(id);
    }


    /**
     * 构建条件查询
     */
    private Specification<Label> createSpecification(Map searchMap) {
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {

                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("labelname") != null && "".equals(searchMap.get("labelname"))){
                    predicateList.add(cb.like(root.get("lablename").as(String.class),"%" +(String) searchMap.get("labelname")+"%"));
                }
                if(searchMap.get("state") != null && "".equals(searchMap.get("state"))){
                    predicateList.add(cb.equal(root.get("state").as(String.class),(String)searchMap.get("state")));
                }
                if(searchMap.get("recommend") != null && "".equals(searchMap.get("recommend"))){
                    predicateList.add(cb.equal(root.get("recommend").as(String.class),(String)searchMap.get("recommend")));
                }
                Predicate[] predicates = predicateList.toArray(new Predicate[predicateList.size()]);
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

}
